/**
 * 
 */
package com.kebo2.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;

/**
 * @author Tim
 *This class adds a new book or user to the database
 */
public class Add {
	
	private VerticalPanel vp;
	private FlexTable ft;
	private TextBox titleTb;
	private TextBox authorTb;
	private TextBox author2Tb;
	private TextBox genreTb;
	private ListBox theme1Cb;
	private ListBox theme2Cb;
	private ListBox ageCb;
	private TextBox priceTb;
	private TextBox placeTb;
	private TextBox publisherTb;
	private TextBox infoTb;
	private ListBox materialCb;
	private ListBox amountListBox;
	private Label titleLb;
	private Label authorLb;
	private Label author2Lb;
	private Label genreLb;
	private Label theme1Lb;
	private Label theme2Lb;
	private Label ageLb;
	private Label priceLb;
	private Label placelb;
	private Label publisherLb;
	private Label infoLb;
	private Label materialLb;
	private Label amountLb;
	private Label usernameLb;
	private Label groupLb;
	private Label adminLb;
	private Label passwordLb;
	private Label topAdminLb;
	private TextBox usernameTb;
	private ListBox groupCb;
	private CheckBox adminCheckBox;
	private PasswordTextBox passwordTb;
	private CheckBox topAdminCheckBox;
	
	public Add(Boolean currentMode) {
		if(currentMode == true)
			bookMode();
		else
			userMode();
	}
	
	private void bookMode() {
		vp = new VerticalPanel();
		ft = new FlexTable();
		
		titleLb = new Label("Titel:");
		ft.setWidget(0, 0, titleLb);
		
		titleTb = new TextBox();
		ft.setWidget(0, 1, titleTb);
		
		authorLb = new Label("Auteur:");
		ft.setWidget(1, 0, authorLb);
		
		authorTb = new TextBox();
		ft.setWidget(1, 1, authorTb);
		
		author2Lb = new Label("Auteur2:");
		ft.setWidget(2, 0, author2Lb);
		
		author2Tb = new TextBox();
		ft.setWidget(2, 1, author2Tb);
		
		genreLb = new Label("Genre:");
		ft.setWidget(3, 0, genreLb);
		
		genreTb = new TextBox();
		ft.setWidget(3, 1, genreTb);
		
		theme1Lb = new Label("Thema1:");
		ft.setWidget(4, 0, theme1Lb);
		
		theme1Cb = new ListBox();
		ft.setWidget(4, 1, theme1Cb);
		theme1Cb.setWidth("161px");
		addThemes();
		
		theme2Lb = new Label("Thema2:");
		ft.setWidget(5, 0, theme2Lb);
		
		theme2Cb = new ListBox();
		ft.setWidget(5, 1, theme2Cb);
		theme2Cb.setWidth("161px");
		
		ageLb = new Label("Leeftijd:");
		ft.setWidget(6, 0, ageLb);
		
		
		ageCb = new ListBox();
		ft.setWidget(6, 1, ageCb);
		ageCb.setWidth("161px");
		ageCb.addItem("+3");
		ageCb.addItem("+6");
		ageCb.addItem("+10");
		ageCb.addItem("+12");
		ageCb.addItem("+14");
		ageCb.addItem("Non fictie");
		ageCb.addItem("Franse fictie");
		ageCb.addItem("Engelse fictie");
		ageCb.addItem("Duitse fictie");
		ageCb.addItem("nvt");
		
		priceLb = new Label("Prijs:");
		ft.setWidget(7, 0, priceLb);
		
		priceTb = new TextBox();
		ft.setWidget(7, 1, priceTb);
		
		placelb = new Label("Plaats:");
		ft.setWidget(8, 0, placelb);
		
		placeTb = new TextBox();
		ft.setWidget(8, 1, placeTb);
		
		publisherLb = new Label("Uitgeverij");
		ft.setWidget(9, 0, publisherLb);
		
		publisherTb = new TextBox();
		ft.setWidget(9, 1, publisherTb);
		
		infoLb = new Label("Verdere Informatie");
		ft.setWidget(10, 0, infoLb);
		
		infoTb = new TextBox();
		ft.setWidget(10, 1, infoTb);
		
		materialLb = new Label("Materiaal");
		ft.setWidget(11, 0, materialLb);
		
		materialCb = new ListBox();
		ft.setWidget(11, 1, materialCb);
		materialCb.setWidth("161px");
		materialCb.addItem("Boek");
		materialCb.addItem("Strip");
		materialCb.addItem("CD");
		materialCb.addItem("DVD");
		materialCb.addItem("CD-ROM");
		materialCb.addItem("Gezelschapsspel");
		
		amountLb = new Label("Aantal:");
		ft.setWidget(12, 0, amountLb);
		
		amountListBox = new ListBox();
		ft.setWidget(12, 1, amountListBox);
		amountListBox.setWidth("50px");
		amountListBox.setVisibleItemCount(3);
		amountListBox.addItem("1");
		amountListBox.addItem("2");
		amountListBox.addItem("3");
		amountListBox.addItem("4");
		amountListBox.addItem("5");
		
		vp.add(ft);
		new UserInterface.Window(vp, "Voeg een boek toe", 300, 500);
		new UserInterface.Icon("save", 4);
		
		ClickHandler handler = new ClickHandler(){
			public void onClick(ClickEvent event) {
				saveMediaInDatabase();
			}
		};
		UserInterface.Icon.saveImg.addClickHandler(handler);
		UserInterface.Icon.saveTxt.addClickHandler(handler);
	}

	protected void saveMediaInDatabase() {
		final String[] data = new String[12];
		int index;
		data[0] = titleTb.getValue();
		data[1] = authorTb.getValue();
		data[2] = author2Tb.getValue();
		data[3] = genreTb.getValue();
		index = theme1Cb.getSelectedIndex();
		data[4] = theme1Cb.getValue(index);
		index = theme2Cb.getSelectedIndex();
		data[5] = theme2Cb.getValue(index);
		index = ageCb.getSelectedIndex();
		data[6] = ageCb.getValue(index);
		data[7] = priceTb.getValue();
		data[8] = placeTb.getValue();
		data[9] = publisherTb.getValue();
		data[10] = infoTb.getValue();
		index = materialCb.getSelectedIndex();
		data[11] = materialCb.getValue(index);
		index = amountListBox.getSelectedIndex();
		data[12] = amountListBox.getValue(index);
		
		if(data[0].length() < 1 || data[1].length() < 1) {
			new UserInterface.notification("Vul minstens een titel en auteur in.");
			return;
		}
		
		String file = "addBook.php?title=" + data[0] + "&author=" + data[1] + "&author2=" + data[2]
		              + "&genre=" + data[3] + "&theme1=" + data[4] + "&theme2=" + data[5] + "&age=" + data[6]
		              + "&price=" + data[7] + "&place=" + data[8] + "&material=" + data[11] + "&publish=" + data[9]
		              + "&amount=" + data[12] + "&info=" + data[10];
		
		HttpRequest object = new HttpRequest();
		object.getMessageXml(file, "GET", null, new AsyncCallback<String>() {
			public void onSuccess(String result) {
				new UserInterface.notification("Media toegevoegd aan de database.");
				UserInterface.closeWindow();
				UserInterface.clearIcons();
			}
			public void onFailure(Throwable caught) {
				new UserInterface.notification(Kebo2.error.INTERNET_CONNECTION);
			}
		});	
	}

	private void addThemes() {
		HttpRequest object = new HttpRequest();
		object.getMessageXml("getThemes.php", "GET", null, new AsyncCallback<String>() {
			public void onSuccess(String result) {
				// TODO Auto-generated method stub	
			}
			public void onFailure(Throwable caught) {
				new UserInterface.notification(Kebo2.error.INTERNET_CONNECTION);		
			}
		});
	}
	
	private void userMode() {
		vp = new VerticalPanel();
		ft = new FlexTable();
		
		usernameLb = new Label("Gebruikersnaam:");
		ft.setWidget(0, 0, usernameLb);
		
		usernameTb = new TextBox();
		ft.setWidget(0, 1, usernameTb);
		
		groupLb = new Label("Groep:");
		ft.setWidget(1, 0, groupLb);
		
		groupCb = new ListBox();
		ft.setWidget(1, 1, groupCb);
		addGroups();
		
		adminLb = new Label("Beheerder?");
		ft.setWidget(2, 0, adminLb);
		
		adminCheckBox = new CheckBox("");
		adminCheckBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				if(event.getValue() == true) {
					passwordLb.setVisible(true);
					topAdminLb.setVisible(true);
					passwordTb.setVisible(true);
					topAdminCheckBox.setVisible(true);
				}
				else {
					passwordLb.setVisible(false);
					topAdminLb.setVisible(false);
					passwordTb.setVisible(false);
					topAdminCheckBox.setVisible(false);
				}
			}
		});
		ft.setWidget(2, 1, adminCheckBox);
		
		passwordLb = new Label("Wachtwoord:");
		passwordLb.setVisible(false);
		ft.setWidget(3, 0, passwordLb);
		
		passwordTb = new PasswordTextBox();
		passwordTb.setVisible(false);
		ft.setWidget(3, 1, passwordTb);
		
		topAdminLb = new Label("Topbeheerder?");
		topAdminLb.setVisible(false);
		ft.setWidget(4, 0, topAdminLb);
		
		topAdminCheckBox = new CheckBox("");
		topAdminCheckBox.setVisible(false);
		ft.setWidget(4, 1, topAdminCheckBox);
		
		vp.add(ft);
		new UserInterface.Window(vp, "Voeg een gebruiker toe", 300, 200);
		new UserInterface.Icon("save", 4);
		
		ClickHandler handler = new ClickHandler() {
			public void onClick(ClickEvent event) {
				saveUserInDatabase();
			}
		};
		UserInterface.Icon.saveImg.addClickHandler(handler);
		UserInterface.Icon.saveTxt.addClickHandler(handler);
	}

	protected void saveUserInDatabase() {
		final String[] data = new String[4];
		int index;
		data[0] = usernameTb.getValue();
		index = groupCb.getSelectedIndex();
		data[1] = groupCb.getValue(index);
		data[2] = Boolean.toString(adminCheckBox.getValue());
		data[3] = passwordTb.getValue();
		data[4] = Boolean.toString(topAdminCheckBox.getValue());
		
		if(data[0].length() < 1) {
			new UserInterface.notification("Vul minstens een gebruikersnaam in.");
			return;
		}
		
		String file = "addUser.php?username=" + data[0] + "&group=" + data[1] + "&admin=" + 
		  			   data[2] + "&password=" + data[3] + "&topadmin=" + data[4];
		
		HttpRequest object = new HttpRequest();
		object.getMessageXml(file, "GET", null, new AsyncCallback<String>() {
			public void onSuccess(String result) {
				new UserInterface.notification("Gebruiker toegevoegd aan de database.");
				UserInterface.closeWindow();
				UserInterface.clearIcons();
			}
			public void onFailure(Throwable caught) {
				new UserInterface.notification(Kebo2.error.INTERNET_CONNECTION);
			}
		});
	}
	
	private void addGroups() {
		HttpRequest object = new HttpRequest();
		object.getMessageXml("getGroups.php", "GET", null, new AsyncCallback<String>() {
			public void onSuccess(String result) {
				// TODO Auto-generated method stub	
			}
			public void onFailure(Throwable caught) {
				new UserInterface.notification(Kebo2.error.INTERNET_CONNECTION);		
			}
		});
	}
}
